package com.coder4j.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SqlLite工具类
 */
public class SqlLiteUtils {

    /**
     * Java类型和Sql类型的对应关系
     */
    private static HashMap<String, String> java2SqlTypeMap = new HashMap<>();

    /**
     * 默认的数据类型
     */
    private static final String DEFAULT_TYPE = "text";

    /**
     * 主键
     */
    private static final String PRIMARY_KEY = "id";

    /**
     * 创建数据表语句的前缀
     */
    private static final String CREATE_PREFIX = "create table if not exists ";

    private static String getSqlLiteType(String type) {
        if (java2SqlTypeMap.isEmpty()) {
            init();
        }
        String str = java2SqlTypeMap.get(type);
        if (StringUtils.isEmpty(str)) {
            return DEFAULT_TYPE;
        }
        return str;
    }

    private static synchronized void init() {
        if (java2SqlTypeMap.isEmpty()) {
            java2SqlTypeMap.put("int", "integer");
            java2SqlTypeMap.put("Integer", "integer");
            java2SqlTypeMap.put("String", "text");
            java2SqlTypeMap.put("long", "integer");
            java2SqlTypeMap.put("Long", "integer");
        }
    }

    /**
     * 获取创建数据表的字符串
     *
     * @param classes
     * @return
     */
    public static List<String> getCreateStringList(Class... classes) {
        List<String> list = new ArrayList<>();
        for (Class clazz : classes) {
            String tableName = getTableName(clazz);
            if(StringUtils.isEmpty(tableName)){
                continue;
            }
            String str = getCreateString(clazz, tableName);
            if (StringUtils.notEmpty(str)) {
                list.add(str);
            }
        }
        return list;
    }

    private static String getTableName(Class clazz){
        return StringUtils.humpToLine(clazz.getSimpleName());
    }

    /**
     * 获取创建数据表的字符串
     *
     * @param clazz
     * @param tableName
     */
    public static String getCreateString(Class clazz, String tableName) {

        Map<String, String> fieldMap = getSqlFieldMap(clazz);

        StringBuilder builder = new StringBuilder();
        builder.append(CREATE_PREFIX).append(tableName).append("(");

        //遍历字段名，拼接创建数据表的语句
        for (String name : fieldMap.keySet()) {
            String type = fieldMap.get(name);
            builder.append(name).append(" ").append(type);
            if (PRIMARY_KEY.equals(name)) {
                builder.append(" primary key");
            }
            builder.append(",");
        }

        return builder.substring(0, builder.length() - 1) + ")";
    }

    public static Map<String,String[]> getTableFieldMap(Class... classes){
        Map<String,String[]> map = new HashMap<>();
        for (Class clazz : classes) {
            String tableName = getTableName(clazz);
            if(StringUtils.isEmpty(tableName)){
                continue;
            }
            Map<String, String> fieldMap = getSqlFieldMap(clazz);
            String[] array = new String[fieldMap.keySet().size()];
            fieldMap.keySet().toArray(array);
            map.put(tableName,array);
        }
        return map;
    }

    /**
     * 获取实体类对应的字段
     * @param clazz
     * @return 数据库字段名:数据库字段类型
     */
    public static Map<String,String> getSqlFieldMap(Class clazz){
        Map<String, Field> javaFieldMap = ReflectUtils.getFieldMap(clazz);
        Map<String,String> sqlFieldMap = new HashMap<>(16);
        for (String name : javaFieldMap.keySet()) {
            String javaType = javaFieldMap.get(name).getType().getSimpleName();
            sqlFieldMap.put(StringUtils.humpToLine(name),getSqlLiteType(javaType));
        }
        return sqlFieldMap;
    }
}
